{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8e636ab7-68c1-4cce-9e21-628fec95779d",
   "metadata": {},
   "source": [
    "## 1、python基础\n",
    "### 1.1、python的输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b93b705f-569a-40ad-83b9-c0bc3a535bc8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello python\n"
     ]
    }
   ],
   "source": [
    "print(\"hello python\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40aa9053-6287-4356-9005-e1d3d159b217",
   "metadata": {},
   "source": [
    "### 1.2、python的变量\n",
    "在c语言中，变量是有数据类型的，声明的时候需要定义数据类型 int a = 10; 表示a是整数\n",
    "在python中，也有数据类型，但是python是弱类型的语言，不用声明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "efc6a76d-02e9-4bb8-b866-8bf24a2ffb9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n",
      "<class 'float'>\n",
      "\n",
      "这是第一行\n",
      "这是第二行\n",
      "这是第三行\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# a是一个整数\n",
    "a = 10\n",
    "print(type(a))\n",
    "a = \"10\"\n",
    "print(type(a))\n",
    "a = 12.3\n",
    "print(type(a))\n",
    "# 由于python是弱类型的语言，如果直接使用一个值，是不会分配内存空间的，所以只要用字符串的方式来定义，不给变量，就是一个注释\n",
    "'这是一段注释'\n",
    "#在python中可以使用三个'''''来定义可以换行的字符串\n",
    "'''\n",
    "这是一段多行注释\n",
    "这是可以换行的\n",
    "'''\n",
    "a = 22\n",
    "s1 = '''\n",
    "这是第一行\n",
    "这是第二行\n",
    "这是第三行\n",
    "'''\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91a27224-c18a-43ec-b931-57c0d7a720ff",
   "metadata": {},
   "source": [
    "### 1.3、python的数据类型\n",
    "#### 1.3.1、python的整数类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a320a11f-2003-43b4-b196-c3e52e5cb2ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2283620624976 <class 'int'> 10\n",
      "2283620624976 <class 'int'> 10\n",
      "2283620625616 <class 'int'> 30 10\n",
      "2283620625936 <class 'int'> 40\n",
      "2283620624976 <class 'int'> 10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(id(a),type(a),a)\n",
    "b = a\n",
    "print(id(b),type(b),b)\n",
    "b = 30\n",
    "#此时b会新建一块内存\n",
    "print(id(b),type(b),b,a)\n",
    "a = 40\n",
    "print(id(a),type(a),a)\n",
    "a = 10\n",
    "#此时a的内存是原来10的那一块\n",
    "print(id(a),type(a),a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c553ae15-6901-456a-a2ab-a67a01fa6bfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2283704398352 1000\n",
      "2283704398448 2000\n",
      "2283704398000 1000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n以上代码中，a完全不一样，是因为在python中，整数和部分字符串有内存的驻留的机制，整数应该是-5-256中的数字会驻留，会存储在单独的空间中，每一次指向这些变量，就不用新分配内存\\n可以有效的节省空间\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1000\n",
    "print(id(a),a)\n",
    "a = 2000\n",
    "print(id(a),a)\n",
    "a = 1000\n",
    "print(id(a),a)\n",
    "'''\n",
    "以上代码中，a完全不一样，是因为在python中，整数和部分字符串有内存的驻留的机制，整数应该是-5-256中的数字会驻留，会存储在单独的空间中，每一次指向这些变量，就不用新分配内存\n",
    "可以有效的节省空间\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de264096-9f9d-48ab-921d-c7340c17e659",
   "metadata": {},
   "source": [
    "#### 1.3.2、小数类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df8038e9-fa42-42d5-b0ef-6015c705666d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2283703270256 <class 'float'> 12.3\n",
      "2283620625040 <class 'int'> 12\n",
      "2283704057040 <class 'float'> 24.3\n"
     ]
    }
   ],
   "source": [
    "a = 12.3\n",
    "print(id(a),type(a),a)\n",
    "b = 12\n",
    "print(id(b),type(b),b)\n",
    "c = a+b\n",
    "#当一个小类型的值(int)，和一个大类型的变量（float）进行运算是，会自动转换为大类型\n",
    "print(id(c),type(c),c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a19f30e-4687-49c9-8a96-814043c1083e",
   "metadata": {},
   "source": [
    "#### 1.3.3、字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b80a6165-0c61-42e6-ab46-76ab39cfed13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'str'> 2283700462384 2283700462384 hello hello\n",
      "2283704451184\n",
      "2283704453872\n",
      "2283704451184\n",
      "2010\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "#字符串类型可以使用单引号或者双引号来定义，python里面建议用单引号\n",
    "s1 = \"hello\"\n",
    "s2 = 'hello'\n",
    "#两个变量指向了同一块内存\n",
    "print(type(s1),type(s2),id(s1),id(s2),s1,s2)\n",
    "s3 = 'abdeed'\n",
    "print(id(s3))\n",
    "s3 = '33333'\n",
    "print(id(s3))\n",
    "s3 = 'abdeed'\n",
    "print(id(s3))\n",
    "\n",
    "s = \"10\"\n",
    "a = 20\n",
    "#此时会报错，在python中不能用字符串和整数进行运算，之所以可以用float和int进行运算，是因为python面向对象的重写机制\n",
    "#s2 = a+s\n",
    "s2 = str(20)+s #需要强制类型转换，将整数转换为字符串\n",
    "print(s2)\n",
    "#将字符串转换为整数\n",
    "a2 = int(s)+a\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8621990b-5b2d-46b9-b104-f16d8543cb61",
   "metadata": {},
   "source": [
    "#### 1.3.4、bool类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e2cfd07c-458a-4a05-ac42-5042ee7d4d12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140728990951528 140728990951560 140728990951528 True False True\n"
     ]
    }
   ],
   "source": [
    "a = True\n",
    "b = False\n",
    "c = True\n",
    "print(id(a),id(b),id(c),a,b,c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8a32844-8c89-4520-bfc2-d710351591c3",
   "metadata": {},
   "source": [
    "### 1.4、输入和输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "47eea066-3dd6-4db1-a0f1-775c84d9f8fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入a的值 10\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 2314257622448 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = input(\"请输入a的值\")\n",
    "#输入的值是个字符串str\n",
    "print(a,id(a),type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7fa29d2-16bc-43fd-a0c9-007d78d89728",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input(\"请输入姓名:\")\n",
    "age = int(input(\"请输入你去年多少岁:\"))\n",
    "#如果希望字符串能够和整数相加，需要进行强制类型转换\n",
    "now_age = age+1\n",
    "print(\"姓名:\"+name+\",年龄:\"+str(now_age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38f76f31-d520-4e2e-a726-c00201bc45bf",
   "metadata": {},
   "source": [
    "每一次输出信息的时候，如果有字符串和整数混合，用普通的+来连接就比较麻烦，所以python中提供了三种方案\n",
    "- 1、通配符的方式\n",
    "- 2、占位符\n",
    "- 3、f通配符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9740d1e-7e64-48c8-ac85-208967eeacba",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input(\"请输入姓名:\")\n",
    "age = int(input(\"请输入你去年多少岁:\"))\n",
    "now_age = age+1\n",
    "print(\"姓名是:%s,当前年龄是:%d,考试成绩%0.2f\"%(name,now_age,87.335))\n",
    "#占位符的方式\n",
    "print(\"姓名是:{},几年{}岁,成绩{}\".format(name,now_age,76.444))\n",
    "#用得最多的是f占位符,这种方式使用最多\n",
    "print(f\"姓名是{name},今年{now_age}岁,考试成绩:{12.333}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d043b663-b233-416b-9197-59a80cbcc765",
   "metadata": {},
   "source": [
    "## 2.运算符\n",
    "### 2.1.运算符基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db9a9e80-f446-4727-b406-1fab9e49497f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1/2) #python的除法不是整除,会自动转换为小数的\n",
    "print(3//2)#整除\n",
    "print(10%4) #取余数\n",
    "print(9%10) #余数是9\n",
    "print(3*4)\n",
    "print(3**3) #表示3的3次方\n",
    "print(\"$\"*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca556f8-46f8-4fc5-905c-19c8d8899457",
   "metadata": {},
   "source": [
    "### 2.2.赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66af5323-fb12-4914-95bf-942efbc93b1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = b = c = 10\n",
    "print(a,b,c)\n",
    "x1 = x2 = x3 = 1000 #三个内存一样\n",
    "print(id(x1),id(x2),id(x3))\n",
    "x1+=0\n",
    "x4 = 1000\n",
    "print(id(x1),id(x4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ccee12b-22f7-4627-bdfb-14b6a2e168a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b = 10,20\n",
    "print(a,b)\n",
    "c,d = 10,10 #只要是在一行中赋值,内存都一样\n",
    "print(id(c),id(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6157ef06-bfbc-4741-abbd-ee7bf1d0dcab",
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b = 10,20\n",
    "a,b = b,a #数值交换\n",
    "print(a,b) #完成了交换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27e52161-55ab-4469-b206-0aa52b61f720",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "a+=10 # a = a+10\n",
    "print(a)\n",
    "# a++ #python里面没有++和--"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9d92b83-58ca-4456-8164-1374d284ccc6",
   "metadata": {},
   "source": [
    "### 2.3.比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e637ecb7-423d-4289-b6ca-8726b5f390ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "#比较运算符得到的结果是bool类型的值\n",
    "a = 100\n",
    "b = 20\n",
    "print(a>=b)\n",
    "c = 10001\n",
    "d = 10001\n",
    "print(c==d) #判断值是否相同\n",
    "print(id(c)==id(d))#比较id是否相同\n",
    "print(c is d)#判断内存是否相同\n",
    "#与或非的运算\n",
    "#c语言中使用的是&&表示与,||表示或,python中没有,一个&是进行位运算\n",
    "# True&&True\n",
    "c = 4&5#一个&是做与的位运算,一个|做或的位运算\n",
    "c = 4|5\n",
    "print(c)\n",
    "#与操作使用and\n",
    "print(True and a>b)\n",
    "print(a<b or a==100)\n",
    "print(not (a<b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d66aee5-125d-43c5-8289-846fee6fb79c",
   "metadata": {},
   "source": [
    "### 2.4.对象的布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06a1fc9c-e256-4386-a892-bcb11cf580b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(False))\n",
    "print(bool(0))\n",
    "print(bool(0.0))\n",
    "print(bool(''))\n",
    "print(bool(None))\n",
    "print(bool(\"\")) #空字符\n",
    "print(bool([])) #空列表\n",
    "print(bool(())) #空元组\n",
    "print(bool({})) #空字典\n",
    "print(bool(dict())) #空字典\n",
    "print(bool(set())) #空集合\n",
    "print(bool(-1)) #-1的bool值是true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffcd87ed-aebc-45c2-ac6b-fc27589d2f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = int(input(\"请输入a:\"))\n",
    "b = int(input(\"请输入b:\"))\n",
    "if b: #b不等于0,因为0的bool值是False\n",
    "    print(a/b)\n",
    "print(\"end\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f2c46e7-d9b3-42a4-b94e-9a7d512efcc0",
   "metadata": {},
   "source": [
    "## 3、语句结构\n",
    "### 3.1、顺序结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0677789e-1f30-4c72-aec1-99720eff362a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "print(a)\n",
    "a+=10 #代码是从上往下顺序执行的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10430347-6dfb-448c-8303-2b028f0a0a62",
   "metadata": {},
   "source": [
    "### 3.2、选择结构\n",
    "#### 3.2.1、选择结构基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46dec8de-fea7-44ea-bb88-3e9d3d961e23",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 101\n",
    "if a==10:\n",
    "    #通过缩进信息来表示代码属于哪一个代码段\n",
    "    print(\"a==10\")\n",
    "    print(30)\n",
    "print('end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f4640a-38f8-4b97-8f9a-f73cf1c4e83d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "if a==10:\n",
    "    pass #pass表示占位符，说明有代码，但是什么都不做"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1f1877b-69bf-4ddb-afcb-f7b080ed84f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "ach = int(input(\"请输入你的成绩:\"))\n",
    "if ach>=90:\n",
    "    print(\"A\")\n",
    "elif ach>=80:\n",
    "    print(\"B\")\n",
    "elif ach>=70:\n",
    "    print(\"C\")\n",
    "elif ach>=60:\n",
    "    print(\"刚刚好\")\n",
    "else:\n",
    "    print(\"挂科了!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4145155-c064-4c3f-8f10-75d3a052a204",
   "metadata": {},
   "outputs": [],
   "source": [
    "saved_money = 1000\n",
    "get_money = int(input(\"请输入要取钱的金额\"))\n",
    "if(saved_money<get_money) :\n",
    "    print(\"你要取的钱余额不足!\")\n",
    "else:\n",
    "    saved_money-=get_money\n",
    "    print(f\"高高兴兴的取走了{get_money}元钱\")\n",
    "print(f'还剩下{saved_money}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d549d8a-8d58-4974-9336-9e8489ccb1de",
   "metadata": {},
   "source": [
    "#### 3.2.2、条件表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7be70086-67a9-4e40-953f-c72c0521fb24",
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b = 30,20\n",
    "#一定要会写\n",
    "print(f\"{a}>{b}\") if a>b else print(f\"{a}<{b}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8463bb2e-3dcb-46ab-b795-780e700ea4fb",
   "metadata": {},
   "source": [
    "### 3.3、循环结构\n",
    "python如果不确定循环次数，使用的是while循环\n",
    "#### 3.3.1、while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bae6be2-7432-4bb5-bc6b-5a8586caffd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = 1\n",
    "while num<100:\n",
    "    if num%2!=0:\n",
    "        print(num,end=',') #以逗号做每次输出的结尾\n",
    "    num+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6224b506-6c7f-43b3-b502-7944a25f95ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum = 0\n",
    "num = 1\n",
    "while num<=100:\n",
    "    sum+=num\n",
    "    num+=1\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21d5d05c-3222-41aa-a9b2-fea0c6a01dcb",
   "metadata": {},
   "source": [
    "#### 3.3.2、for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fde4299b-b8a0-4c3d-ae01-748d75f96d2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "range(10) #表示会产生一个迭代器，里面是0-9的数字，python中范围操作的逻辑:包头不包尾 range(10)相当于range(0,10)\n",
    "for i in range(10):\n",
    "    print(i,end=\",\")\n",
    "print(\"*\"*100)\n",
    "for i in range(10,21):\n",
    "    print(i)\n",
    "r1 = range(10)\n",
    "r2 = range(10,1000)\n",
    "print(id(r1),type(r1),sys.getsizeof(r1))\n",
    "print(id(r2),type(r2),sys.getsizeof(r2))\n",
    "#所占内存空间的大小是相同的，这也就是range对象的意义，只有在具体的遍历的时候才会分配内存，所以使用range可以节省内存空间\n",
    "for i in r1:\n",
    "    print(i)\n",
    "print('*'*100)\n",
    "lst = list(range(10,100))#如果利用range生成一个列表对象，此时就会根据大小分配内存\n",
    "print(sys.getsizeof(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10225202-fb5c-4e2a-bc9a-9d62620e8660",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(f\"{j}*{i}={j*i}\",end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04a0bfb9-b90d-4249-9e67-4e10c03cae30",
   "metadata": {},
   "outputs": [],
   "source": [
    "layer = 16\n",
    "for i in range(1,layer+1):\n",
    "    #星号的数量\n",
    "    snum = 2*i-1\n",
    "    #空格的数量\n",
    "    space_num = layer-i\n",
    "    #输出空格，不换行\n",
    "    for j in range(space_num):\n",
    "        print(' ',end='')\n",
    "    #输出*号\n",
    "    for j in range(snum):\n",
    "        print(\"*\",end='')\n",
    "    print()\n",
    "#输出树干\n",
    "for i in range(1,layer+1):\n",
    "    space_num = layer-1\n",
    "    for j in range(space_num):\n",
    "        print(' ',end='')\n",
    "    print('*')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aeab1d9-225b-40d9-8045-261f74cab83f",
   "metadata": {},
   "source": [
    "#### 3.3.3、break和continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36425320-5d6f-4115-9e07-2164dbfda588",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,10):\n",
    "    if i%5==0:\n",
    "        # continue #表示跳过本次循环，跳转到条件判断\n",
    "        break #完全跳出循环\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66e81719-9007-45c3-833d-0fffafcb182b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#python中的循环有else\n",
    "for i in range(1,11):\n",
    "    if i%5==0:\n",
    "        # break\n",
    "        continue\n",
    "    print(i)\n",
    "else:\n",
    "    print(\"end for\")\n",
    "#循环中的else会在正常结束循环之后执行，如果没有正常结束，break之后就不会执行else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb729433-9b27-4ae2-b463-c6c3e42942fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取钱，输入密码，如果三次输入失败，就锁卡\n",
    "pwd = '123'\n",
    "for i in range(3):\n",
    "    s = input(\"请输入密码:\")\n",
    "    if s==pwd:\n",
    "        print(\"密码正确!\")\n",
    "        break;\n",
    "    else:\n",
    "        # if i==2:\n",
    "        #     print(\"卡被锁住了\")\n",
    "        #     break\n",
    "        print(\"密码出错，请再次输入密码!\")\n",
    "else:\n",
    "    print(\"卡被锁住了!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdfdce85-a911-4cbf-ac69-6b85f593ec3d",
   "metadata": {},
   "source": [
    "## 4、python的集合类\n",
    "### 4.1、python的列表\n",
    "#### 4.1.1、列表的基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cbbe4b3-5ddb-4a63-935a-d86f3cbbc92e",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [10,'abc',12.3,True] #python中可以在一个列表中存不同的元素\n",
    "print(id(lst),type(lst))\n",
    "print(id(lst[0]),type(lst[0]))\n",
    "print(id(lst[1]),type(lst[1]))#0和1的最终的内存地址并没有连续\n",
    "for item in lst:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "688ed562-0edf-4d78-b6af-74d85090e0bd",
   "metadata": {},
   "source": [
    "#### 4.1.2、列表的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91c8a1b1-9c54-4b4a-8982-02a9055e5ef8",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = list([1,2,3,4]) #直接构造一个元素\n",
    "print(lst,type(lst))\n",
    "lst = list(range(10,100)) #直接使用range来构造，注意和range的区别是，range不会马上分配内存，但是list会马上分配内存\n",
    "import sys\n",
    "print(sys.getsizeof(range(10,100)),sys.getsizeof(lst),type(lst))\n",
    "#列表生成式:结构--> 表达式   for循环\n",
    "lst = list(x for x in range(1,11)) # 相当于lst = list(range(1,11))\n",
    "lst = list(x**2 for x in range(1,11)) #列表中存储了1-10的平方\n",
    "print(lst)\n",
    "#也可以把列表生成式直接写在中括号中\n",
    "lst = [x**2 for x in range(1,11) if x%2==0]#如果x是偶数才进行生成，列表生成式的筛选机制\n",
    "print(lst)\n",
    "#如果不想筛选，而是希望偶数是平方，奇数是3次方\n",
    "lst = [x**2 if x%2==0 else x**3 for x in range(1,11)]\n",
    "print(lst)\n",
    "\n",
    "lst = []\n",
    "for i in range(1,11):\n",
    "    if i%2==0:\n",
    "        lst.append(i**2)\n",
    "    else:\n",
    "        lst.append(i**3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad2cc1fc-976c-4395-9d3f-6e6a5f5b244d",
   "metadata": {},
   "source": [
    "#### 4.1.3、列表的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "676dcbe4-aa59-4a50-bb63-f2418204137b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[11]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[11, 13]\n",
      "[10, 12, 14, 16, 18]\n",
      "[10, 13]\n",
      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10]\n",
      "[19, 17, 15, 13, 11]\n",
      "[18, 16, 14, 12, 10]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10,20))\n",
    "print(lst)\n",
    "#切片的格式 lst[start:end:step],默认的start是0，step1,end默认位最后\n",
    "lst2 = lst[1:2:1]#切片\n",
    "print(lst2) #包头不包尾\n",
    "print(lst)#切片之后会生成一个新的列表，不会修改原列表\n",
    "print(lst[1:5:2])\n",
    "print(lst[::2])\n",
    "print(lst[:5:3])\n",
    "print(lst[::-1])\n",
    "#逆序输出偶数位\n",
    "print(lst[::-2])\n",
    "#逆序输出偶数\n",
    "print(lst[-2::-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9518b335-a006-4576-8d98-15ab9650ee51",
   "metadata": {},
   "source": [
    "- 取出偶数下表的元素\n",
    "- 反转字符串\n",
    "- 隔两个取一位\n",
    "- 倒序偶数位\n",
    "- 列表中提取奇数位元素并且反向输出\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09f0e1db-f0b9-4caa-9326-8325a27bb4af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 12, 14, 16, 18]\n",
      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10]\n",
      "[10, 13, 16, 19]\n",
      "[18, 16, 14, 12, 10]\n",
      "[19, 17, 15, 13, 11]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = list(range(10,20))\n",
    "print(lst[::2])\n",
    "print(lst[::-1])\n",
    "print(lst[::3])\n",
    "print(lst[-2::-2])\n",
    "#进行了两次切片，这种写法非常常用，在数据分析中，容易和二维数组搞混\n",
    "print(lst[1::2][::-1])\n",
    "lst = [[1,2,3],[3,4,5],[6,7,8]]\n",
    "lst[0][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aa4921c-30fa-4f97-95ca-96ada21e5a34",
   "metadata": {},
   "source": [
    "#### 4.1.4、列表的常用操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ca30bbaa-a1fa-4b3c-9b41-aea52972394f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] 2314284051328\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 111] 2314284051328\n",
      "2314284051328 2314284051328\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 111, 119] [1, 2, 3, 4, 5, 6, 7, 8, 9, 111, 119] 2314284051328 2314284051328\n",
      "10 20\n"
     ]
    }
   ],
   "source": [
    "#添加列表\n",
    "lst = list(range(1,10))\n",
    "print(lst,id(lst))\n",
    "lst.append(111)\n",
    "#list是可变对象，添加元素之后，不会分配新的内存\n",
    "print(lst,id(lst))\n",
    "lst2 = lst\n",
    "print(id(lst2),id(lst))\n",
    "lst2.append(119)\n",
    "#由于list是可变对象，所以lst2添加的时候是在原对象上添加\n",
    "print(lst,lst2,id(lst),id(lst2))\n",
    "#int是不可变对象，在修改b的时候，会新分配内存\n",
    "a = 10\n",
    "b = a\n",
    "b = 20\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "20dc19ac-9049-4b6c-b472-c26becd5d607",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f']\n",
      "['a', 'b', 'd', 'e', 'f']\n",
      "['a', 'd', 'e', 'f']\n"
     ]
    }
   ],
   "source": [
    "#列表删除\n",
    "lst = ['a','b','c','d','e','f','g']\n",
    "lst.pop() #删除最后一个元素\n",
    "print(lst)\n",
    "lst.pop(2) #根据下标删除元素\n",
    "print(lst)\n",
    "# lst.pop(10) #删除不存在的元素会抛出异常\n",
    "lst.remove('b')#根据元素来删除\n",
    "print(lst)\n",
    "# lst.remove('aa')#如果删除没有的元素会抛出异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "bc8bb46c-05f1-4173-b919-62d53ad91ad5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[1, 2, 22, 3, 4, 5, 6, 7, 8, 9]\n",
      "[]\n",
      "[1, 2, 100, 200, 300, 22, 3, 4, 5, 6, 7, 8, 9]\n",
      "200 8\n",
      "5\n",
      "1\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#列表的插入\n",
    "lst = list(range(1,10))\n",
    "print(lst)\n",
    "lst.insert(2,22)\n",
    "print(lst)\n",
    "# lst.insert(3,[100,200,300])\n",
    "#等于在第三个位置直接插入了list对象\n",
    "# print(lst)\n",
    "# lst[1:] = [100,200,300] #从1开始切片，把后面的元素全部替换位100,200,300\n",
    "# print(lst)\n",
    "print(lst[2:2])#start从2开始，一个元素都没有切\n",
    "lst[2:2] = [100,200,300] #非常重要，像一个列表中插入数组的方式，利用切片\n",
    "print(lst)\n",
    "#获取列表中某个位置的元素\n",
    "print(lst[3],lst[-2])\n",
    "#根据元素来获取位置\n",
    "print(lst.index(22))#根据元素来获取位置\n",
    "print(lst.index(2))#获取2这个元素的位置\n",
    "# lst.index(33)#获取不存在，会抛出异常\n",
    "print(33 in lst) #判断33有没有在列表中\n",
    "print(22 in lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3530ce9-04b6-4ab9-ab1d-20a6651c3bb4",
   "metadata": {},
   "source": [
    "### 4.2、字典\n",
    "字典存储的是键值对，使用{'a':10,'b':20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e63d66f4-fbe7-47d4-9a37-25c06d1a7c82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b': 20, 'c': 40}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a':10,'b':20}\n",
    "d['a']\n",
    "d['a'] = 30#会修改a的值为30\n",
    "d['a']\n",
    "d['c'] = 40\n",
    "d\n",
    "#判断键是否存在\n",
    "'a' in d\n",
    "#删除一个键\n",
    "d.pop('a')\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3b701bde-73b6-4ebe-abbc-51b8b542c219",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'username': '张三', 'password': '123', 'age': 22}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user = {'username':'张三','password':'123','age':22} #json\n",
    "user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4c0bfc33-e95d-41a4-bb3f-dc5b7b9a3391",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:10\n",
      "b:20\n",
      "c:30\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "d = dict() #直接通过构造函数创建\n",
    "d = {'a':10,'b':20,'c':30}\n",
    "#获取字典的键,会把键转换为列表\n",
    "d.keys()\n",
    "#字典的遍历\n",
    "for key in d.keys():\n",
    "    print(f\"{key}:{d[key]}\")\n",
    "#获取字典中所有的值\n",
    "d.values()\n",
    "# d.clear() #清空字典\n",
    "# d\n",
    "#获取字典中的值\n",
    "d['a']\n",
    "# d['bb'] #获取没有的值会抛出异常\n",
    "#如果不希望抛出异常，可以使用get方法来获取\n",
    "d.get('a')\n",
    "print(d.get('aa')) #如果字典中这个值不存在，会返回None。此时不用处理异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5d69ded7-5fe9-4bd4-bb35-64c513563711",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 10, 10.0)\n",
      "('b', 20, 20.22)\n",
      "('c', 30, 30.33)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'aa': 30, 'ba': 60, 'ca': 90}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst1 = ['a','b','c']\n",
    "lst2 = [10,20,30]\n",
    "lst3 = [10.0,20.22,30.33]\n",
    "for i in zip(lst1,lst2,lst3):\n",
    "    print(i)\n",
    "# d = dict(表达式  for循环)#中间写生成式\n",
    "d = {k:v for k,v in zip(lst1,lst2)}\n",
    "d = {k+'a':v*3 for k,v in zip(lst1,lst2)}\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5920e9c-bc29-4975-bbeb-c842be776e52",
   "metadata": {},
   "source": [
    "### 4.3、用户管理系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1cd497d4-10fb-4e69-bccc-4fe9be71c6e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢迎使用用户管理系统\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: A\n",
      "请输入用户名: caocao\n",
      "请输入用户昵称: 曹操\n",
      "请输入用户年龄: 33\n",
      "请输入密码: 123123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当!添加用户成功!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: A\n",
      "请输入用户名: lb\n",
      "请输入用户昵称: 刘备\n",
      "请输入用户年龄: 23\n",
      "请输入密码: 3123123123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当!添加用户成功!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: a\n",
      "请输入用户名: sq\n",
      "请输入用户昵称: 孙权\n",
      "请输入用户年龄: 32\n",
      "请输入密码: 232323\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当!添加用户成功!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: a\n",
      "请输入用户名: guojia\n",
      "请输入用户昵称: 郭嘉\n",
      "请输入用户年龄: 23\n",
      "请输入密码: 123123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当!添加用户成功!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: F\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户名     用户昵称    用户年龄    用户名密码     \n",
      "caocao      曹操      33          123123    \n",
      "lb          刘备      23          3123123123\n",
      "sq          孙权      32          232323    \n",
      "guojia      郭嘉      23          123123    \n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: d\n",
      "请输入用户名： dd\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*************************************************************>删除的用户不存在！<*************************************************************\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: d\n",
      "请输入用户名： lb\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当！删除用户成功<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: f\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户名     用户昵称    用户年龄    用户名密码     \n",
      "caocao      曹操      33          123123    \n",
      "sq          孙权      32          232323    \n",
      "guojia      郭嘉      23          123123    \n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: u\n",
      "请输入用户名： ss\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*************************************************************>更新的用户不存在!<*************************************************************\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: u\n",
      "请输入用户名： sq\n",
      "请输入用户昵称: 老孙\n",
      "请输入用户年龄: \n",
      "请输入用户密码: 33333333\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->当当当,更新用户成功!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: q\n",
      "请输入用户名: sq\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户名     用户昵称    用户年龄    用户名密码     \n",
      "sq          老孙      32          33333333  \n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: f\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户名     用户昵称    用户年龄    用户名密码     \n",
      "caocao      曹操      33          123123    \n",
      "sq          老孙      32          33333333  \n",
      "guojia      郭嘉      23          123123    \n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: l\n",
      "请输入登录用户 dd\n",
      "请输入密码 23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*************************************************************>用户名或者密码不正确!<*************************************************************\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: l\n",
      "请输入登录用户 caocao\n",
      "请输入密码 1231231\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*************************************************************>用户名或者密码不正确。<*************************************************************\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: l\n",
      "请输入登录用户 caocao\n",
      "请输入密码 123123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->欢迎曹操使用我们的系统!<--------------------------------------------------------------\n",
      "请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择正确的操作: e\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------->欢迎再次使用我们的系统!<--------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# user = {'username':'zhangsan','nickname':'张三','age':22,'password':'123'}\n",
    "users = []\n",
    "def show_title():\n",
    "   format_title = '{:<8}{:<8}{:<8}{:<10}'\n",
    "   print(format_title.format('用户名','用户昵称','用户年龄','用户名密码'))\n",
    "\n",
    "def show_data(user):\n",
    "    format_title = '{:<12}{:<8}{:<12}{:<10}'\n",
    "    print(format_title.format(user['username'],user['nickname'],user['age'],user['password']))\n",
    "\n",
    "def show_operator():\n",
    "    print(\"请选择一个正确的操作：A(添加)|U(更新)|D(删除)|F(列表)|L(登录)|Q(查询)|E(退出)\")\n",
    "\n",
    "def user_query(username):\n",
    "    #循环遍历整个列表\n",
    "    for u in users:\n",
    "        #如果用户名存在就返回数据\n",
    "        if u['username']==username:\n",
    "            return u\n",
    "    #不存在返回None\n",
    "    return None\n",
    "\n",
    "#错误信息显示\n",
    "def error_msg(msg):\n",
    "    print(f'*************************************************************>{msg}<*************************************************************')\n",
    "\n",
    "#显示正确信息\n",
    "def suc_msg(msg):\n",
    "    print(f'------------------------------------------------------------->{msg}<--------------------------------------------------------------')\n",
    "\n",
    "def user_add():\n",
    "    #1、输入用户名\n",
    "    username = input('请输入用户名:')\n",
    "    #2、判断用户是否存在，存在不能添加\n",
    "    u = user_query(username)\n",
    "    if u:\n",
    "        error_msg('添加的用户已经存在！不能添加')\n",
    "        return\n",
    "    #3、输入密码，用户昵称，年龄\n",
    "    nickname = input('请输入用户昵称:')\n",
    "    age = input('请输入用户年龄:')\n",
    "    password = input('请输入密码:')\n",
    "    #4、添加用户\n",
    "    u = {}\n",
    "    u['username'] = username\n",
    "    u['nickname'] = nickname\n",
    "    u['age'] = int(age)\n",
    "    u['password'] = password\n",
    "    users.append(u)\n",
    "    suc_msg(\"当当当!添加用户成功!\")\n",
    "\n",
    "def find_users():\n",
    "    show_title()\n",
    "    for u in users:\n",
    "        show_data(u)\n",
    "\n",
    "def user_delete():\n",
    "    #1、输入用户名\n",
    "    username = input('请输入用户名：')\n",
    "    #2、获取用户\n",
    "    u = user_query(username)\n",
    "    if u==None:\n",
    "        error_msg('删除的用户不存在！')\n",
    "        return\n",
    "    users.remove(u)\n",
    "    suc_msg(\"当当当！删除用户成功\")\n",
    "\n",
    "def user_update():\n",
    "    #1、查询\n",
    "    #1.1、输入用户名\n",
    "    username = input('请输入用户名：')\n",
    "    #1.2、获取用户对象\n",
    "    u = user_query(username)\n",
    "    if u==None:\n",
    "        error_msg('更新的用户不存在!')\n",
    "        return\n",
    "    #2、输入其他信息，如果为空默认不更新\n",
    "    nickname = input('请输入用户昵称:')\n",
    "    age = input('请输入用户年龄:')\n",
    "    password = input('请输入用户密码:')\n",
    "    if nickname:\n",
    "        u['nickname'] = nickname\n",
    "    if age:\n",
    "        u['age'] = int(age)\n",
    "    if password:\n",
    "        u['password'] = password\n",
    "    suc_msg('当当当,更新用户成功!')\n",
    "\n",
    "def user_login():\n",
    "    #1、输入用户名\n",
    "    username = input('请输入登录用户')\n",
    "    #2、输入密码\n",
    "    password = input('请输入密码')\n",
    "    #3、判断是否能够登录\n",
    "    ##3.1、根据用户名获取用户对象，如果不存在说，用户名不存在\n",
    "    u = user_query(username)\n",
    "    if u==None:\n",
    "        error_msg(\"用户名或者密码不正确!\")\n",
    "        return\n",
    "    ##3.2、判断密码是否正确\n",
    "    if u['password']!=password:\n",
    "        error_msg('用户名或者密码不正确。')\n",
    "        return\n",
    "    ##3.3、显示结果\n",
    "    suc_msg(f'欢迎{u[\"nickname\"]}使用我们的系统!')\n",
    "\n",
    "def main():\n",
    "    #输出一个欢迎\n",
    "    print(\"欢迎使用用户管理系统\")\n",
    "    #如果希望程序不结束，必须添加一个死循环\n",
    "    while True:\n",
    "        #显示操作\n",
    "        show_operator()\n",
    "        #获取输入值\n",
    "        op = input(\"请选择正确的操作:\")\n",
    "        if op.lower()=='a':\n",
    "            user_add()\n",
    "        elif op.lower()=='u':\n",
    "            user_update()\n",
    "        elif op.lower()=='d':\n",
    "            user_delete()\n",
    "        elif op.lower()=='f':\n",
    "            find_users()\n",
    "        elif op.lower()=='q':\n",
    "            username = input('请输入用户名:')\n",
    "            u = user_query(username)\n",
    "            if u:\n",
    "                show_title()\n",
    "                show_data(u)\n",
    "            else:\n",
    "                error_msg('查询的用户不存在!')\n",
    "        elif op.lower()=='l':\n",
    "            user_login()\n",
    "        elif op.lower()=='e':\n",
    "            suc_msg(\"欢迎再次使用我们的系统!\")\n",
    "            break;\n",
    "        else:\n",
    "            error_msg(\"请选择一个正确的操作!\")\n",
    "    \n",
    "main()\n",
    "# show_title()\n",
    "# show_data({'username':'zs','nickname':'张三','age':22,'password':'123'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f5c32bcc-3af2-40e5-bb9b-7d26ec8f95df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'Hello'\n",
    "a.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0cbcfec-7969-4a0e-aa26-9d80652d5f68",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
